﻿using LiveCharts;
using LoveNote.Application.Share.Enums;
using LoveNote.Application.Share.Login;
using LoveNote.Database.Entities;
using LoveNote.Database.Interfaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using TookKit.Mvvm.Attributes;
using TookKit.Mvvm.Bindings;
using TookKit.Mvvm.Enums;

namespace LoveNote.Application.Main.ViewModels
{
    [DependencyInjection(Lifetime.Singleton)]
    internal class IndexViewModel:ObservableObject
    {
        private ObservableCollection<NoteEntity> notes = new ObservableCollection<NoteEntity>();
        public ObservableCollection<NoteEntity> Notes
        {
            get { return notes; }
            set { SetProperty(ref notes, value); }
        }

        private ObservableCollection<WorkEntity> works = new ObservableCollection<WorkEntity>();
        public ObservableCollection<WorkEntity> Works
        {
            get { return works; }
            set { SetProperty(ref works, value); }
        }

        private ObservableCollection<CustomerEntity> topCustomers = new ObservableCollection<CustomerEntity>();
        public ObservableCollection<CustomerEntity> TopCustomers
        {
            get { return topCustomers; }
            set { SetProperty(ref topCustomers, value);}
        }

        private List<CustomerEntity> customers;
        public List<CustomerEntity> Customers
        {
            get { return customers; }
            set { SetProperty(ref customers, value); }
        }
        private List<ProcessEntity> processes;
        public List<ProcessEntity> Processes
        {
            get { return processes; }
            set { SetProperty(ref processes, value);}
        }

        private ObservableCollection<PictureEntity> pictures = new ObservableCollection<PictureEntity>();
        public ObservableCollection<PictureEntity> Pictures
        {
            get { return pictures; }
            set { SetProperty(ref pictures, value); }
        }

        private int sumCustomer = 0;
        public int SumCustomer
        {
            get { return sumCustomer; }
            set { SetProperty(ref sumCustomer, value); }
        }

        private double sumMoney = 0.0;
        public double SumMoney
        {
            get { return sumMoney; }
            set { SetProperty(ref sumMoney, value); }
        }

        private int sumPicture = 0;
        public int SumPicture
        {
            get { return sumPicture; }
            set { SetProperty(ref sumPicture, value); }
        }

        private int sumWork = 0;
        public int SumWork
        {
            get { return sumWork; }
            set { SetProperty(ref sumWork, value); }
        }

        private int sumWorking = 0;
        public int SumWorking
        {
            get { return sumWorking; }
            set { SetProperty(ref sumWorking, value); }
        }

        private IChartValues customerValues = new ChartValues<double>();
        /// <summary>
        /// 客户数量每月统计
        /// </summary>
        public IChartValues CustomerValues
        {
            get { return customerValues; }
            set { SetProperty(ref customerValues, value); }
        }

        private IChartValues moneyValues = new ChartValues<double>();
        /// <summary>
        /// 客户金额每月统计
        /// </summary>
        public IChartValues MoneyValues
        {
            get { return moneyValues; }
            set { SetProperty(ref moneyValues, value); }
        }

        private ObservableCollection<string> customerLabels = new ObservableCollection<string>();

        /// <summary>
        ///客户每月统计的X轴标签
        /// </summary>
        public ObservableCollection<string> CustomerLabels
        {
            get { return customerLabels; }
            set { SetProperty(ref customerLabels, value); }
        }



        public ICommand LoadedCommand { get; }
        public ICommand CompleteWorkCommand { get; }

        public ISession Session { get; }
        private INoteRepository NoteRepository { get; }
        private IWorkRepository WorkRepository { get; }
        private ICustomerRepository CustomerRepository { get; }
        private IPictureRepository PictureRepository { get; }
        private IProcessRepository ProcessRepository { get; }

        public IndexViewModel(ISession session,
            INoteRepository noteRepository,
            IWorkRepository workRepository,
            ICustomerRepository customerRepository,
            IPictureRepository pictureRepository,
            IProcessRepository processRepository)
        {
            Session = session;
            NoteRepository = noteRepository;
            WorkRepository = workRepository;
            CustomerRepository = customerRepository;
            PictureRepository = pictureRepository;
            ProcessRepository = processRepository;
            LoadedCommand = new RelayCommand(OnLoadedCommand);
            CompleteWorkCommand = new RelayCommand<WorkEntity>(OnCompleteWorkCommand);
            
        }

        private void OnCompleteWorkCommand(WorkEntity entity)
        {
            if (entity != null)
            {
                entity.Complete = (int)CompleteType.已完成;
                entity.CompleteDate = DateTime.Now;
                WorkRepository.Update(entity);
                LoadWorks(10);
            }
        }

        private void OnLoadedCommand()
        {
            LoadProcesses();
            LoadNotes(6);
            LoadWorks(10);
            LoadCustomers();
            LoadPictures();
        }

        private void LoadProcesses()
        {
            Processes = ProcessRepository.GetAll()
                .OrderByDescending(t => t.InsertDate)
                .ToList();
        }

        private void LoadNotes(int count)
        {
            Notes.Clear();
            var notes = NoteRepository.GetAll()
                .FindAll(t => t.UserId == Session.CurrentUser.Id)
                .OrderByDescending(t => t.InsertDate)
                .ToList();
            foreach (var item in notes)
            {
                if (count-- <= 0) break;
                Notes.Add(item);
            }
        }

        private void LoadWorks(int count)
        {

            Works.Clear();
            var works = WorkRepository.GetAll()
                .FindAll(t => t.UserId == Session.CurrentUser.Id)
                .OrderByDescending(t => t.InsertDate)
                .ToList();
            SumWork = works.Count;
            SumWorking = works.Where(t => t.Complete == 0).Count();
            foreach (var work in works)
            {
                if (count-- <= 0) 
                    break;
                if(work.Complete == 0)
                    Works.Add(work);                
            }
        }
        private void LoadCustomers()
        {

            Customers = CustomerRepository.GetAll()
                .FindAll(t => t.UserId == Session.CurrentUser.Id)
                .OrderByDescending(t => t.UpdateDate)
                .ToList();
            SumCustomer = Customers.Count;
            SumMoney = Customers.Sum(t => t.Money);

            //客户报表统计:今年1月至12月统计        
            CustomerLabels.Clear();
            CustomerValues.Clear();
            MoneyValues.Clear();
            for (int i = 1; i <= 12; i++)
            {
                CustomerLabels.Add($"{i}月");
                double count = Customers.FindAll(t => t.InsertDate.Year == DateTime.Now.Year && t.InsertDate.Month == i).Count();
                double sumMoney = Customers.FindAll(t => t.InsertDate.Year == DateTime.Now.Year && t.InsertDate.Month == i).Sum(t => t.Money);
                CustomerValues.Add(count);
                MoneyValues.Add(sumMoney);
            }

            //客户最新动态
            TopCustomers.Clear();
            foreach (var customer in Customers)
            {
                TopCustomers.Add(customer);
                customer.Process = Processes.FirstOrDefault(process => process.CustomerId == customer.Id);
                if (TopCustomers.Count == 3) 
                    break;
            }

        }


        private void LoadPictures(int count = 20)
        {
            var pictures = PictureRepository.GetAll()
                .FindAll(t => t.UserId == Session.CurrentUser.Id)
                .OrderByDescending(t => t.InsertDate)
                .ToList();
            SumPicture = pictures.Count;

            Pictures.Clear();
            foreach (var picture in pictures)
            {
                if (count-- <= 0) break;
                Pictures.Add(picture);
            }
        }

    }
}
